package com.jh.storageview;

import static com.jh.storageview.StorageFragment.STORAGE_APP;
import static com.jh.storageview.StorageFragment.STORAGE_DOC;
import static com.jh.storageview.StorageFragment.STORAGE_IMG;
import static com.jh.storageview.StorageFragment.STORAGE_PKG;
import static com.jh.storageview.StorageFragment.STORAGE_SOUND;
import static com.jh.storageview.StorageFragment.STORAGE_SYS_D;
import static com.jh.storageview.StorageFragment.STORAGE_SYS_G;
import static com.jh.storageview.StorageFragment.STORAGE_VIDEO;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

public class StorageViewGroup extends LinearLayout implements StorageFragment.StorageFragmentListenerPreferenceLoc {

    public static final String TAG = "StorageViewGroup";

    Context                  mContext;
    StorageView              mStorageView;
    StorageViewLine          mStorageViewLine;
    PointF mPointF;

    public StorageViewGroup(Context context) {
        super(context);
    }

    public StorageViewGroup(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public StorageViewGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    public StorageViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        init();
    }

    StorageHelper helper;
    private void init(){
        setGravity(Gravity.CENTER|Gravity.TOP);
        setPadding(0, 100, 0, 0);
        mPointF = new PointF();
        View view = LayoutInflater.from(mContext).inflate(R.layout.storage_viewgroup, null);
        mStorageView = view.findViewById(R.id.storage_view);
        mStorageViewLine = view.findViewById(R.id.storage_view_line);
        StorageFragment storageFragment = new StorageFragment();
        ((AppCompatActivity)mContext).getSupportFragmentManager().beginTransaction()
                        .replace(R.id.frame_content, storageFragment)
                        .commit();

        mStorageView.addObserver(storageFragment);

        helper = new StorageHelper(mStorageView, storageFragment);

        storageFragment.setSFLoc(this);
        mStorageView.setTouchOvalListener(new StorageView.TouchOvalListener() {
            @Override
            public void callback(boolean[] touchOval) {
                mStorageViewLine.setItemArrays(touchOval);
            }

            @Override
            public void pointCall(PointF pointF) {
                mStorageViewLine.setPointF(pointF);
            }

            @Override
            public void touchUp() {
                mStorageViewLine.clearLine();
            }
        });

        addView(view);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }

    @Override
    public void localCb(int[] dy) {
        Log.d(TAG, "localCb: dy");
        mStorageViewLine.setPointRF(dy);
    }

    public StorageHelper getHelper(){
        return helper;
    }
}
